package com.nikhu.fx.services;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.nikhu.fx.bom.Currency;
import com.nikhu.fx.bom.ExchangeRate;
import com.nikhu.fx.integration.ExchangeRateService;

@WebService(endpointInterface = "com.nikhu.fx.services.CurrencyConverter")
public class CurrencyConverterImpl implements CurrencyConverter {

	private ExchangeRateService exchangeRateService;

	public ExchangeRateService getExchangeRateService() {
		return exchangeRateService;
	}

	public void setExchangeRateService(ExchangeRateService exchangeRateService) {
		this.exchangeRateService = exchangeRateService;
	}

	private static List<Currency> currencies = new ArrayList<Currency>();
	private static List<ExchangeRate> exchangeRates = new ArrayList<ExchangeRate>();
	static {
		currencies.add(new Currency("AUD", "Australian Dollar", "AUS",
				"Australia"));
		currencies.add(new Currency("USD", "American Dollar", "USA",
				"United States of America"));

		exchangeRates.add(new ExchangeRate("USD", "INR", 54.24, Calendar
				.getInstance().getTime()));
		exchangeRates.add(new ExchangeRate("AUD", "INR", 55.43, Calendar
				.getInstance().getTime()));
	}

	public List<Currency> currencies() {
		return currencies;
	}

	public List<ExchangeRate> currencyRates() {
		return exchangeRateService.findAll();
	}

	public double convertAmount(String fromCurrency, String toCurrency,
			double amount) {
		ExchangeRate fromExchangeRate = null;
		ExchangeRate toExchangeRate = null;
		for (ExchangeRate exchangeRate : exchangeRates) {
			if (fromCurrency.equalsIgnoreCase(exchangeRate.getSymbol())) {
				fromExchangeRate = exchangeRate;
			}
			if (toCurrency.equalsIgnoreCase(exchangeRate.getSymbol())) {
				toExchangeRate = exchangeRate;
			}
		}
		return fromExchangeRate.getRate();
	}
}
